package com.pmp.activiti.editor;

import com.pmp.vo.OrgChart;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.converter.util.InputStreamProvider;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.impl.util.io.InputStreamSource;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @description: 解析bpmn文件并转换为orgchart模型
 * @author: pmq
 * @time: 2017-06-11 16:49
 */

public class BpmnModelConvert2OrgChart {

    /**
     * @description: 通过流程文件解析出OrgChart集合
     * @author: pmq
     * @time: 2017/6/11 17:20
     * @param: []
     * @return: java.util.List<com.pmp.vo.OrgChart>
     */
    public List<OrgChart> bpmnConvertoModel() {
        String resouce = "pmp2017613.bpmn";
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resouce);
        BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
        InputStreamProvider inputStreamProvider = new InputStreamSource(inputStream);
        BpmnModel bpmnModel = bpmnXMLConverter.convertToBpmnModel(inputStreamProvider, true, true, "UTF-8");
        System.out.println(bpmnModel);
        Process process = bpmnModel.getProcesses().get(0);
        Collection<FlowElement> flowElements = process.getFlowElements();
        List<UserTask> userTasks = new ArrayList<>();
        for (FlowElement e : flowElements) {
            if (e instanceof SequenceFlow || e instanceof ExclusiveGateway || e instanceof StartEvent || e instanceof EndEvent)
                continue;
            userTasks.add((UserTask) e);
        }
        if (userTasks != null && userTasks.size() > 0) {
            return getOrgChartByUserTask(userTasks);
        }
        return null;
    }

    /**
     * @description：根据流程任务节点抽象出orgChart数据结构
     * @author: 彭明强
     * @time: 2017/6/11 17:20
     * @param: [userTasks]
     * @return: java.util.List<com.pmp.vo.OrgChart>
     */
    public List<OrgChart> getOrgChartByUserTask(List<UserTask> userTasks) {
        List<OrgChart> orgCharts = new ArrayList<>();
        for (int i=0; i<userTasks.size(); i++) {
            UserTask u = userTasks.get(i);
            OrgChart orgChart = new OrgChart();
            orgChart.setId(u.getId());
            orgChart.setKeyId(u.getId());
            orgChart.setName(u.getName());
            if (i==2)
                orgChart.setClassName("processOk");
            else
                orgChart.setClassName("pipeline1");
            List<FormProperty> formProperties = u.getFormProperties();
            for (FormProperty f : formProperties) {
                List<FormValue> formValues = f.getFormValues();
                for (FormValue fv : formValues) {
                    orgChart.setParentKeyId(fv.getId());
                }
            }
            orgCharts.add(orgChart);
        }
        return orgCharts;
    }

    /**
     * @description：构造orgChart结构的集合
     * @author: pmq
     * @time: 2017/6/11 17:20
     * @param: [root, list]
     * @return: com.pmp.vo.OrgChart
     */
    public OrgChart constructOrgChart(OrgChart root, List<OrgChart> list) {
        List<OrgChart> childList = new ArrayList<OrgChart>();
        for (int i = 0; i < list.size(); i++) {
            OrgChart org = list.get(i);
            if (root.getKeyId().equals(org.getParentKeyId())) {
                OrgChart orgChart = new OrgChart();
                orgChart.setId(org.getId());
                orgChart.setKeyId(org.getKeyId());
                orgChart.setParentKeyId(org.getParentKeyId());
                orgChart.setName(org.getName());
                orgChart.setClassName(org.getClassName());
                org.setParent(root);
                childList.add(orgChart);
            }
        }
        root.setChildren(childList);
        for (int y = 0; y < childList.size(); y++) {
            constructOrgChart(childList.get(y), list);
        }
        return root;
    }
}
